﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace EntityFrameworkDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            var _TempDB1 = new TempDB1Entities6();

            using (var tran = _TempDB1.Database.BeginTransaction())
            {
                try
                {

                    Table1 _table = _TempDB1.Table1.Where(u => u.ID == 4).FirstOrDefault();
                    _table.Test1 = "8888";

                    _TempDB1.Entry(_table).State = System.Data.Entity.EntityState.Added;
                    _TempDB1.SaveChanges();

                    // throw new Exception("人工异常");

                    Table1 _table2 = _TempDB1.Table1.Where(u => u.ID == 3).FirstOrDefault();
                    _table2.Test1 = "6666";
                    _TempDB1.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    tran.Rollback();
                }
            }




            Console.ReadKey();
        }

        static int Add()
        {
            var _TempDB1 = new TempDB1Entities6();

            List<Table1> _List = new List<Table1>()
            {
                new Table1()
                {
                    ID = 3,
                    Test1 = "3333",
                    Test2 = "3333",
                    Test3 = "3333",
                    Test4 = "3333"

                },
                new Table1()
                {
                    ID = 4,
                    Test1 = "4444",
                    Test2 = "4444",
                    Test3 = "4444",
                    Test4 = "4444"

                },
            };

            //Table1 _table = new Table1();
            //_table.ID = 2;
            //_table.Test1 = "2222";
            //_table.Test2 = "2222";
            //_table.Test3 = "2222";
            //_table.Test4 = "2222";

            //_TempDB1.Set<Table1>().Add(_table);
            //_TempDB1.Table1.Add(_table);

            _TempDB1.Set<Table1>().AddRange(_List);
            //_TempDB1.Table1.Add(_table);

            return _TempDB1.SaveChanges();
        }

        static int Remove1()
        {
            var _TempDB1 = new TempDB1Entities6();

            Table1 _table = new Table1();
            _table.ID = 3;

            _TempDB1.Table1.Attach(_table);
            _TempDB1.Table1.Remove(_table);

            return _TempDB1.SaveChanges();
        }
        static int Remove2()
        {
            var _TempDB1 = new TempDB1Entities6();

            Table1 _table = _TempDB1.Table1.Where(u => u.ID == 4).FirstOrDefault();

            if (_table == null)
                return 0;

            // _TempDB1.Table1.Attach(_table);
            _TempDB1.Table1.Remove(_table);

            return _TempDB1.SaveChanges();
        }

        static int AddOrUpdate1()
        {
            var _TempDB1 = new TempDB1Entities6();

            Table1 _table = new Table1();
            _table.ID = 5;
            _table.Test1 = "Update33";
            // 未修改的属性，将为NULL

            _TempDB1.Table1.AddOrUpdate(_table);

            return _TempDB1.SaveChanges();

        }
        static int AddOrUpdate2()
        {
            var _TempDB1 = new TempDB1Entities6();

            Table1 _table = _TempDB1.Table1.Where(u => u.ID == 4).FirstOrDefault();
            _table.Test1 = "Update44";
            // 未修改的属性，保留原值；

            _TempDB1.Table1.AddOrUpdate(_table);

            return _TempDB1.SaveChanges();

        }

        static int Update1()
        {
            var _TempDB1 = new TempDB1Entities6();

            Table1 _table = new Table1();
            _table.ID = 4;

            _TempDB1.Table1.Attach(_table);
            _table.Test1 = "1234";

            return _TempDB1.SaveChanges();
        }
        static int Update2()
        {
            var _TempDB1 = new TempDB1Entities6();

            Table1 _table = _TempDB1.Table1.Where(u => u.ID == 4).FirstOrDefault();

            // _TempDB1.Table1.Attach(_table);
            _table.Test1 = "Update44";

            return _TempDB1.SaveChanges();
        }

        static int StateAdd()
        {
            var _TempDB1 = new TempDB1Entities6();

            Table1 _table = new Table1();
            _table.ID = 6;
            _table.Test1 = "6666";
            _table.Test2 = "6666";
            _table.Test3 = "6666";
            _table.Test4 = "6666";

            _TempDB1.Entry(_table).State = System.Data.Entity.EntityState.Added;
            return _TempDB1.SaveChanges();
        }
        static int StateDelete()
        {
            var _TempDB1 = new TempDB1Entities6();

            Table1 _table = new Table1();
            _table.ID = 6;

            _TempDB1.Entry(_table).State = System.Data.Entity.EntityState.Deleted;
            return _TempDB1.SaveChanges();
        }

        static int StateModify()
        {
            var _TempDB1 = new TempDB1Entities6();

            Table1 _table = new Table1();
            _table.ID = 6;
            _table.Test1 = "1234";

            _TempDB1.Entry(_table).State = System.Data.Entity.EntityState.Modified;
            return _TempDB1.SaveChanges();
        }


        static void Method1()
        {
            var _TempDB1 = new TempDB1Entities6();
            var Listgroup = _TempDB1.Table1.GroupBy(u=>u.Test1);
            foreach (var group in Listgroup)
            {
                foreach (var item in group)
                {

                }
            }
        }
    }
}
